package com.linewell.system.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.linewell.commom.dao.impl.BaseDaoImpl;
import com.linewell.system.dao.ISysDepartmentDao;
import com.linewell.system.model.SysDepartment;
/**
 * 具体实现SysDepartment的CRUD
 * 
 * */
@Repository
public class SysDepartmentDaoImpl extends BaseDaoImpl<SysDepartment> implements ISysDepartmentDao {

	@Override
	public List<SysDepartment> getAllDepaetment(Integer type,Integer depId) {
		//String hql = " FROM SysDepartment d WHERE d.status = 9 AND FIND_IN_SET(d.depId,getDepChildIds(1000))>0 ";
		
		StringBuffer hql = new StringBuffer();
				
		hql.append(" FROM SysDepartment d WHERE d.status = 9 ");
		
		if(type != null &&  !"".equals(type)){
			hql.append(" AND d.depType = "+type.byteValue()+"");
		}
		
		if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(d.depId,getChildDepId("+depId+")) > 0 ");
		}
		
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return  query.list();
	}

	@Override
	public boolean updateAll(SysDepartment c) {
		
		StringBuffer hql = new StringBuffer(
				"update SysDepartment  set ");
		
		//拼接修改条件


		hql.append("  depPId = "+c.getDepPId()+" ");
		hql.append(" , depType = "+c.getDepType().byteValue()+" ");
		
		if(c.getDepName() != null && !"".equals(c.getDepName())){
			hql.append(" , depName = '"+c.getDepName()+"' ");
		}

		if(c.getAddress() != null && !"".equals(c.getAddress())){
			hql.append(" , address = '"+c.getAddress()+"' ");
		}
		if(c.getX() != null && !"".equals(c.getX())){
			hql.append("  , x = "+c.getX()+" ");
		}
		if(c.getY() != null && !"".equals(c.getY())){
			hql.append(" , y = "+c.getY()+" ");
		}
		if(c.getPhone() != null && !"".equals(c.getPhone())){
			hql.append(" , phone = "+c.getPhone()+" ");
		}
		if(c.getDesction() != null && !"".equals(c.getDesction()) ){
			hql.append(" , desction = '"+c.getDesction()+"' ");
		}
		if(c.getStatus() != null && !"".equals(c.getStatus())){
			hql.append("  , status = "+c.getStatus()+" ");
		}
		if(c.getIcon() != null && !"".equals(c.getIcon())){
			hql.append("  , icon = '"+c.getIcon()+"' ");
		}

		hql.append(" where depId ="+c.getDepId()+" ");
		
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql.toString());
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean remove(Integer id) {
		String hql = "update SysDepartment s set s.status = 0 where s.depId in ("+id+") or s.depPId in ("+id+")";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	@Override
	public boolean deleteAll(Integer id) {
		String hql = "delete SysDepartment s where s.depId in ("+id+") or s.depPId in ("+id+")";
		Query query = super.sessionFactory.getCurrentSession().createQuery(hql);
		return (query.executeUpdate() > 0);
	}

	

}
